﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BookShop.BLL;
using BookShop.DTO;
using BookShop.Library.Common;
using BookShop.Library.Constant;
using PagedList;

namespace BookShop.Admin.Controllers
{
    public class OrderController : Controller
    {
        public ActionResult Add()
        {
            var mod = (Moderator)Session[SessionKey.Moderator];
            if (mod == null)
            {
                return RedirectToAction("Login", "Home");
            }
            if (!CommonFunction.CheckModeratorRole(mod, ModeratorRole.OrderWrite))
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.UnauthorizedAction;
                return RedirectToAction("List");
            }
            return View();
        }

        [HttpPost]
        public ActionResult Add(long? customerId)
        {
            var mod = (Moderator)Session[SessionKey.Moderator];
            if (mod == null)
            {
                return RedirectToAction("Login", "Home");
            }
            if (!CommonFunction.CheckModeratorRole(mod, ModeratorRole.OrderWrite))
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.UnauthorizedAction;
                return RedirectToAction("List");
            }
            var order = new Order { CustomerId = customerId, CreateTime = DateTime.Now, Status = OrderStatus.New };
            var addResult = OrderService.Add(order);
            if (addResult != null)
            {
                return RedirectToAction("Detail", new { orderId = order.Id });
            }
            TempData[AlertMessage.TempDataKey] = AlertMessage.ErrorOccurs;
            ViewBag.AddCustomerId = customerId;
            return View();
        }

        public ActionResult Delete(long orderId)
        {
            var mod = (Moderator)Session[SessionKey.Moderator];
            if (mod == null)
            {
                return RedirectToAction("Login", "Home");
            }
            if (!CommonFunction.CheckModeratorRole(mod, ModeratorRole.OrderWrite))
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.UnauthorizedAction;
                return RedirectToAction("List");
            }
            var deleteResult = OrderService.Delete(orderId);
            if (!deleteResult)
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.ErrorOccurs;
            }
            return RedirectToAction("List");
        }

        public ActionResult Detail(long orderId)
        {
            var mod = (Moderator)Session[SessionKey.Moderator];
            if (mod == null)
            {
                return RedirectToAction("Login", "Home");
            }
            if (!CommonFunction.CheckModeratorRole(mod, ModeratorRole.OrderRead))
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.UnauthorizedAction;
                return RedirectToAction("List");
            }
            var order = OrderService.GetById(orderId);
            if (order != null)
            {
                if (order.CustomerId.HasValue)
                {
                    order.CustomerName = (CustomerService.GetById(order.CustomerId.Value) ?? new Customer()).FullName;
                }
                return View(order);
            }
            TempData[AlertMessage.TempDataKey] = AlertMessage.NotFoundObject;
            return RedirectToAction("List");
        }

        public ActionResult Edit(long orderId)
        {
            var mod = (Moderator)Session[SessionKey.Moderator];
            if (mod == null)
            {
                return RedirectToAction("Login", "Home");
            }
            if (!CommonFunction.CheckModeratorRole(mod, ModeratorRole.OrderWrite))
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.UnauthorizedAction;
                return RedirectToAction("List");
            }
            var order = OrderService.GetById(orderId);
            if (order != null)
            {
                return View(order);
            }
            TempData[AlertMessage.TempDataKey] = AlertMessage.NotFoundObject;
            return RedirectToAction("List");
        }

        [HttpPost]
        public ActionResult Edit(long orderId, long? customerId, int status)
        {
            var mod = (Moderator)Session[SessionKey.Moderator];
            if (mod == null)
            {
                return RedirectToAction("Login", "Home");
            }
            if (!CommonFunction.CheckModeratorRole(mod, ModeratorRole.OrderWrite))
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.UnauthorizedAction;
                return RedirectToAction("List");
            }
            var order = OrderService.GetById(orderId);
            if (order == null)
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.NotFoundObject;
                return RedirectToAction("List");
            }
            order.CustomerId = customerId;
            order.Status = status;
            var editResult = OrderService.Update(order);
            if (editResult != null)
            {
                return RedirectToAction("Detail", new { orderId = order.Id });
            }
            TempData[AlertMessage.TempDataKey] = AlertMessage.ErrorOccurs;
            return View(order);
        }

        [ValidateInput(false)]
        public ActionResult List(long? orderId, long? customerId, string fromCreateTime, string toCreateTime,
             int? status, string fromTotalPrice, string toTotalPrice, int page = 1, int pageSize = 20)
        {
            var mod = (Moderator)Session[SessionKey.Moderator];
            if (mod == null)
            {
                return RedirectToAction("Login", "Home");
            }
            if (!CommonFunction.CheckModeratorRole(mod, ModeratorRole.OrderRead))
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.UnauthorizedAction;
                return RedirectToAction("Index", "Home");
            }
            int totalRow;
            var model = OrderService.Search(orderId, customerId, fromCreateTime, toCreateTime, status, fromTotalPrice,
                toTotalPrice, page, pageSize, out totalRow);
            ViewBag.SearchId = orderId.ToString();
            ViewBag.SearchCustomerId = customerId.ToString();
            ViewBag.SearchFromCreateTime = fromCreateTime ?? string.Empty;
            ViewBag.SearchToCreateTime = toCreateTime ?? string.Empty;
            ViewBag.SearchFromTotalPrice = fromTotalPrice ?? string.Empty;
            ViewBag.SearchToTotalPrice = toTotalPrice ?? string.Empty;
            ViewBag.SearchStatus = status.ToString();
            ViewBag.PageList = new StaticPagedList<Order>(model, page, pageSize, totalRow);
            ViewBag.RecordsCount = "Tổng số bản ghi: " + totalRow;
            if (model.Count != 0)
            {
                foreach (var order in model)
                {
                    if (order.CustomerId.HasValue)
                    {
                        order.CustomerName = (CustomerService.GetById(order.CustomerId.Value) ?? new Customer()).FullName;
                    }
                }
            }
            return View(model);
        }

        public JsonResult GetAllCustomer()
        {
            return Json(CustomerService.GetAll());
        }
    }
}
